import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pkg_obz/tools/date_formatter.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'common_widgets.dart';

/// Create： Zhang 4/1/21 1:08 PM
/// 描述：封装一些通用的控件
///获取一个带有下拉加载更多的listView

///加载更多回调
typedef LoadingMore = void Function();

///下拉刷新
typedef Refresh = void Function();

///封装下拉刷新listView使用
typedef ListViewItemBuilder = Widget Function(BuildContext context, int index);

///带下拉加载的listView
refreshListView({
  ///创建listView的item
  required ListViewItemBuilder itemBuilder,

  ///下拉刷新回调方法
  required Refresh refresh,

  ///下拉刷新控制器
  required RefreshController controller,

  ///item的总数
  required int itemCount,

  ///上拉加载回调方法
  LoadingMore? onLoadingMore,

  ///listView的内边距
  EdgeInsetsGeometry? padding,

  ///分隔线的高度
  double dividerHeight = 1,

  ///分隔线的颜色
  Color dividerColor = Colors.black12,

  ///下拉刷新的loading圆环的颜色
  Color refreshColor = Colors.blue,

  ///是否开启上拉加载
  bool isNeedLoadingMore = false,
}) {
  return SmartRefresher(
    controller: controller,
    enablePullUp: isNeedLoadingMore,
    physics: const BouncingScrollPhysics(),
    header: WaterDropHeader(
      waterDropColor: refreshColor,
      refresh: loadingView(
        size: 20,
        color: refreshColor,
      ),
      complete: Text(
        DateFormatter.format(
          DateTime.now().millisecondsSinceEpoch,
          formatter: DateFormatter.formatter_03,
        ),
        style: TextStyle(
          color: refreshColor,
          fontSize: 14,
          fontWeight: FontWeight.w600,
        ),
      ),
    ),
    footer: ClassicFooter(
      loadStyle: LoadStyle.ShowWhenLoading,
      loadingText: "",
      canLoadingText: "松手加载",
      idleText: "加载完毕",
      loadingIcon: loadingView(
        size: 20,
        color: Colors.grey,
      ),
      idleIcon: Container(),
    ),
    child: ListView.separated(
      padding: padding,
      itemCount: itemCount,
      itemBuilder: (context, index) {
        return itemBuilder(context, index);
      },
      separatorBuilder: (BuildContext context, int index) => Divider(
        height: dividerHeight,
        color: dividerColor,
      ),
    ),
    onRefresh: refresh,
    onLoading: onLoadingMore,
  );
}
